package com.marktotop.controller;


import com.marktotop.vo.LoginDTO;
import com.marktotop.entity.Admin;
import com.marktotop.response.ResultBean;
import com.marktotop.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @author WXLD
 * @data 2021/6/6 - 9:16
 */
@RestController
@Api(tags = "系统登录接口")
public class LoginController {

    @Autowired
    private IAdminService adminService;

    /**
     * 登录方法
     *
     * @param loginUser 登录实体类
     * @param request
     * @return
     */
    @ApiOperation(value = "用户登录",notes="POST提交，返回Token")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResultBean login(@RequestBody LoginDTO loginUser, HttpServletRequest request){
        return adminService.login(loginUser.getUsername(), loginUser.getPassword(),request);
    }


    /**
     * 获取登录用户信息
     *
     * @param principal
     * @return
     */
    @ApiOperation(value = "获取当前登录用信息",notes="GET提交，返回登录用户信息")
    @RequestMapping(value = "/getProfile",method = RequestMethod.GET)
    public ResultBean getLoginUserProfile(Principal principal){
        // 没有登录信息
        if (null == principal){
            return ResultBean.error();
        }
        String loginName = principal.getName();
        Admin admin = adminService.getProfileByUsername(loginName);
        admin.setPassword(null);
        admin.setRoles(adminService.getRoles(admin.getId()));
        return ResultBean.ok().data("loginUser",admin);
    }


    /**
     * 用户退出
     *
     * @return
     */
    @ApiOperation(value = "退出登录",notes="POST提交，退出登录")
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public ResultBean logout(){
        return ResultBean.ok();
    }


}
